package com.example.foobar.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.foobar.business.service.DaRetentionRatesService;
import com.example.foobar.dao.service.DaRetentionRatesDaoService;
import com.example.foobar.dto.RetentionRateDTO;
import com.example.foobar.entity.DaRetentionRates;
import com.example.foobar.mapper.DaRetentionRatesMapper;
import com.example.foobar.param.DaRetentionRatesPageParam;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import io.geekidea.springbootplus.framework.core.pagination.PageInfo;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 留存率记录 服务实现类
 *
 * @author 郑英奔
 * @since 2021-06-04
 */
@Slf4j
@Service
public class DaRetentionRatesDaoServiceImpl extends BaseServiceImpl<DaRetentionRatesMapper, DaRetentionRates> implements DaRetentionRatesDaoService {

    @Autowired
    private DaRetentionRatesMapper daRetentionRatesMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveDaRetentionRates(DaRetentionRates daRetentionRates) {
        return super.save(daRetentionRates);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateDaRetentionRates(DaRetentionRates daRetentionRates) {
        return super.updateById(daRetentionRates);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteDaRetentionRates(Long id) {
        return super.removeById(id);
    }

    @Override
    public Paging<DaRetentionRates> getDaRetentionRatesPageList(DaRetentionRatesPageParam daRetentionRatesPageParam)  {
        Page<DaRetentionRates> page = new PageInfo<>(daRetentionRatesPageParam, OrderItem.desc(getLambdaColumn(DaRetentionRates::getCreateTime)));
        LambdaQueryWrapper<DaRetentionRates> wrapper = new LambdaQueryWrapper<>();
        IPage<DaRetentionRates> iPage = daRetentionRatesMapper.selectPage(page, wrapper);
        return new Paging<DaRetentionRates>(iPage);
    }

    @Override
    public List<RetentionRateDTO> getStroyRetentonRates(String uniacid, Date startDate, Date endDate) {
        Map<String, Object> params = new HashMap<>();
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        params.put("uniacid",uniacid);
        List<RetentionRateDTO> list = daRetentionRatesMapper.getStroyRetentonRates(params);
        return list;
    }

    @Override
    public int selectCount(LambdaQueryWrapper<DaRetentionRates> wrapper) {
        return daRetentionRatesMapper.selectCount( wrapper);
    }

}
